<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<link rel="stylesheet" href="./lib/bootstrap-3.3.7.css">

<body>
    <div id="app">

        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">添加品牌</h3>
            </div>
            <div class="panel-body form-inline">
                <label>
                    Id:
                    <input type="text" class="form-control" v-model="id">
                </label>

                <label>
                    Name:
                    <input type="text" class="form-control" v-model="name">
                </label>

                <input type="button" value="添加" class="btn btn-primary" @click="add()">

                <label>
                    搜索名称关键字：
                    <input type="text" class="form-control" v-model="keywords">
                </label>
            </div>
        </div>


        <table class="table table-bordered table-hover table-striped">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Ctime</th>
                    <th>Operation</th>
                </tr>
            </thead>
            <tbody>
                <!-- 之前， v-for 中的数据，都是直接从 data 上的list中直接渲染过来的 -->
                <!-- 现在， 我们自定义了一个 search 方法，同时，把 所有的关键字，通过传参的形式，传递给了 search 方法 -->
                <!-- 在 search 方法内部，通过 执行 for 循环， 把所有符合 搜索关键字的数据，保存到 一个新数组中，返回 -->
                <tr v-for="(item,index) in search(keywords)" :key="item.id">
                    <td>{{ item.id }}</td>
                    <td v-text="item.name"></td>
                    <td>{{ item.ctime | dateFormat('yyyy-mm-dd')}}</td>
                    <td>
                        <a href="" @click.prevent="del(index)">删除</a>
                    </td>
                </tr>
            </tbody>
        </table>

    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        Vue.filter('dateFormat', function (dateStr, pattern = "") {
            let date = new Date(dateStr);
            let y = date.getFullYear();
            let m = date.getMonth();
            let d = date.getDate() + 1;
            let h = date.getHours();
            let mm = date.getMinutes();
            let s = date.getSeconds();
            if (pattern.toLowerCase() === 'yyyy-mm-dd') {
                return `${y}-${m}-${d}`;
            }
            return `${y}-${m}-${d} ${h}:${mm}:${s}`;
        });
        let app = new Vue({
            el: '#app',
            data: {
                id: '',
                name: '',
                keywords: '',
                list: [
                    { id: 1, name: '奔驰', ctime: new Date() },
                    { id: 2, name: '宝马', ctime: new Date() }
                ]
            },
            methods: {
                del(index) {
                    this.list.splice(index, 1);
                },
                add() {
                    this.list.push({ id: this.id, name: this.name, ctime: new Date() })
                },
                search(name) {
                    return this.list.filter(e => {
                        return e.name.includes(name);
                    })
                }
            }
        })
    </script>
</body>

</html>